package com.cai.blog.controller;


import com.cai.blog.common.Cache;
import com.cai.blog.common.aop.LogAnnotation;
import com.cai.blog.service.ArticleService;
import com.cai.blog.vo.Result;
import com.cai.blog.vo.params.ArticleParam;
import com.cai.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articles")
public class ArticleController {

    /**
     *
     * @param 首页文章列表
     * @return
     */
    @Autowired
    private  ArticleService articleService;
    @PostMapping
    @LogAnnotation(module="文章",operation="获取文章列表")
    public Result listArticle(@RequestBody PageParams pageParams){
        return articleService.listArticle(pageParams);

    }

    /**
     * 最热文章
     * @return
     */
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle(){
        int limit = 5;
        return articleService.hotArticle(limit);
    }
    @PostMapping("new")
    public Result newArticle(){
        int limit = 5;
        return articleService.newArticle(limit);
    }
    @PostMapping("listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }
    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){

        return articleService.findArticleById(articleId);
    }
    /**
     *    @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；
     *     而最常用的使用请求体传参的无疑是POST请求了，所以使用@RequestBody接收数据时，一般都用POST方式进行提交。
     */

    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);

    }

}
